/**
 * 
 */
package com.iwords.web.action;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

import com.iwords.constants.Constants;
import com.iwords.enums.KnownLevel;
import com.iwords.service.SysLibServiceLocal;
import com.iwords.service.UserServiceLocal;
import com.iwords.service.object.SysLibEntity;
import com.iwords.service.object.UserEntity;
import com.iwords.web.base.BaseAction;

/**
 * @author    	huangbaichuan 
 * @time      	2011-8-6    
 * @description	cookie初始化  
 */
@WebServlet(name = "index", urlPatterns = {"/index"})
public class Index extends BaseAction<UserEntity> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @EJB
    UserServiceLocal userService;
    @EJB
    SysLibServiceLocal sysLibService;

    public void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {
        if (req.getSession().getAttribute("user") != null) {

            UserEntity user = (UserEntity) req.getSession().getAttribute("user");
            user = userService.find(user.getUserid());
            SysLibEntity currentLib = null;
            if (user.getCurrentlib() != null) {
                currentLib = sysLibService.findSysLibById(user.getCurrentlib());
                currentLib.setPercent(sysLibService.getStudyLibNum(user.getUserid(), user.getCurrentlib(), KnownLevel.Known.getValue()) * 100
                        / currentLib.getQuantity());
                req.setAttribute("currentLib", currentLib);
            }  


        } else {

            Cookie cs[] = req.getCookies();
            if (cs != null) {
                for (Cookie c : cs) {
                    if (c.getName().equals("userid")) {
                        UserEntity user = userService.find(Long.parseLong(c.getValue()));
                        if (user != null) {
                            req.getSession().setAttribute("user", user);
                            SysLibEntity currentLib = null;
                            if (user.getCurrentlib() != null) {
                                currentLib = sysLibService.findSysLibById(user.getCurrentlib());
                                currentLib.setPercent(sysLibService.getStudyLibNum(user.getUserid(), user.getCurrentlib(), KnownLevel.Known.getValue()) * 100
                                        / currentLib.getQuantity());
                                req.setAttribute("currentLib", currentLib);
                            }
                        }

                    }

                }
            }

        }
        this.forward(req, res, Constants.INDEX_PAGE_JSP);

    }

    public UserEntity initEntity(HttpServletRequest req) {
        return null;
    }
}
